package com.itwill.ecom.product;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.itwill.ecom.board.ExistedBoardException;
import com.itwill.ecom.cart.CartBeans;
import com.itwill.ecom.cart.CartService;
import com.itwill.ecom.member.MemberBeans;
import com.itwill.ecom.member.MemberService;

@Controller
@RequestMapping(value = "/product")
public class ProductController {
	@Autowired
	private ProductService productService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private CartService cartService;

	
	String msg = "";

	// 상품 추가
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(@ModelAttribute ProductBeans beans, Model model) {
		System.out.println(">>>>>>>>>>>>>>상품 등록작성");
		String forwardPath = "";
		boolean result = false;
		try {
			productService.createProduct(beans);
			forwardPath = "product/product_create";
		} catch (ExistedBoardException e) {
			model.addAttribute("msg", e.getMessage());
			model.addAttribute("fproduct", beans);
			forwardPath = "product/productList";
			e.printStackTrace();
		} catch (Exception e) {
			forwardPath = "include/404";
			e.printStackTrace();
		}
		return forwardPath;
	}

	// 상품 전체
	@RequestMapping(value = "/productList")
	public String selectAll(List<ProductBeans> productList,	Model model) {
		String forwardPath = "";
		System.out.println(">>>>>>>>>>>>>>>>상품 다 찾을래");
		try {
			productList = productService.getProductAll();
			model.addAttribute("productList", productList);
			forwardPath = "product/product_list";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return forwardPath;
	}

	// 카테고리별or 제조사별 상품
	@RequestMapping(value = "/categoryList", method = RequestMethod.GET)
	public String categoryList(
			@RequestParam(value = "categoryno") int category_no, Model model) {
		String forwardPath = "";
		System.out.println(">>>>>>>>>>>>>>>>해당 카테고리 상품 찾을래");
		try {
			List<ProductBeans> categoryList = productService
					.getProductByCategory(category_no);
			model.addAttribute("categoryList", categoryList);
			forwardPath = "product/product_categoryList";
		} catch (ProductNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return forwardPath;
	}

	//상품찾기(회사번호)
	@RequestMapping(value = "/companyList", method = RequestMethod.GET)
	public String companyList(
			@RequestParam(value = "companyno") int company_no, Model model) {
		String forwardPath = "";
		System.out.println(">>>>>>>>>>>>>>>>해당 제조사 상품 찾을래");
		try {
			List<ProductBeans> companyList = productService
					.getProductByCompany(company_no);
			model.addAttribute("companyList",companyList);
			forwardPath = "product/product_companyList";
		} catch (ProductNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return forwardPath;
	}

	//관리자 상품 업데이트 페이지
	@RequestMapping(value = "/updateProduct", method = RequestMethod.GET)
	public String updateProduct(@ModelAttribute ProductBeans beans, Model model) {
		System.out.println(">>>>>>>>>>>>>>>>상품정보 수정할래");
		String forwardPath = "";
		try {
			productService.updateProduct(beans);
			forwardPath = "product/productList";
			model.addAttribute("beans",beans);
			forwardPath = "product/test/update";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return forwardPath;
	}	

	// 상품 삭제
	@RequestMapping(value = "/deleteProduct", method = RequestMethod.GET)
	public String deleteProduct() {
		System.out.println(">>>>>>>>>>>>>>>>상품 지울래");
		
		return "product/test/delete";
	}

	// 상품상세보기
	@RequestMapping(value = "/productDetail", method = RequestMethod.GET)
	public String detail(@RequestParam(value="productno")int product_no, ProductBeans beans, Model model) {
		String forwardPath = "";
		System.out.println(">>>>>>>>>>>>>>>>디테일 진입");
		try {
			
			beans = productService.getProductByNo(product_no);
			
			forwardPath = "product/product_detail";

			System.out.println("상품번호 : " + beans.getProduct_no());
			System.out.println("상품명 : " + beans.getProduct_name());
			System.out.println("상품회사 : " + beans.getProduct_company());
			System.out.println("상품가격 : " + beans.getProduct_price());
			System.out.println("카테고리 : " + beans.getProduct_category());

			model.addAttribute(beans);

		} catch (ProductNotFoundException e) {
			System.out.println("상품 낫 퐈운드 익셉숀");
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return forwardPath;
	}

	// 관리자 상품 삭제
	@RequestMapping(value = "/productDelete", method = RequestMethod.GET)
	public String delete(ProductBeans beans, Model model) {
		try {
			productService.deleteProduct(beans.getProduct_no());
			msg = "delete";
			model.addAttribute("msg", msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/product/productList";
	}
	
	
	// 장바구니에 담기
	@RequestMapping(value="/productdetailaction")
	public String productdetailaction(@RequestParam("sid_id") String id, @RequestParam("product_no") int pn, HttpServletRequest req) throws Exception{
		//세션에 아이디가 있는지 확인해보고 없으면 로그인폼으로 이동합니다.
		if(id == null||id.equals("null")){
			return "member/login_form";			
		}		
		MemberBeans memberBeans = memberService.getMemberById(id);;
		int mn = memberBeans.getMember_no(); //member_no 뽑아내기
		int issame = 0; //미중복0 중복1
		req.setAttribute("sid_no", mn); //request에 실었음
		
		/** 한아이디에 한물품만 넣을 수 있게 함(중복불가) **/
		List<CartBeans> cartBeansList = null;
		//Exception cartService에서 넘어와서 임시로 잡아놈
		try{
			cartBeansList = cartService.getCartListByMemberNo(mn);
		} catch(Exception e){
			e.printStackTrace();
		}
		
		for(CartBeans cartBeans : cartBeansList){ //아이디에 똑같은 상품이 있으면
			if((cartBeans.getCart_member()==mn) && (cartBeans.getCart_product() == pn)){
				issame = 1; //중복
			}
		}
		req.setAttribute("issame", issame);
		issame=0;
		/*****************************************************/
		
		return "product/product_detail_action";
	}
}
